#include "scenemark.h"
#include "ui_scenemark.h"
#include <QMessageBox>
#include <QDebug>
#include <QMimeData>
#include <QDrag>
#include <QMouseEvent>
#include <QInputDialog>

SceneMark::SceneMark(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SceneMark)
{
    this->ChannelID=0;
    this->isconfig=false;
    getUI();
//    ui->setupUi(this);
//    ui->LCD_Value->setSegmentStyle(QLCDNumber::Flat);
//    QPalette lcdpat = ui->LCD_Value->palette();
//    lcdpat.setColor(QPalette::Normal,QPalette::WindowText,Qt::blue);
//    ui->LCD_Value->setPalette(lcdpat);
//    this->setAcceptDrops(true);
//    this->setAttribute(Qt::WA_DeleteOnClose);
//    this->ChannelID=-1;
//    this->statescene.setBackgroundBrush(QBrush(Qt::blue));
//    ui->GV_State->setScene(&this->statescene);
}

void SceneMark::getUI()
{
    ui->setupUi(this);
    ui->LCD_Value->setSegmentStyle(QLCDNumber::Flat);
    QPalette lcdpat = ui->LCD_Value->palette();
    lcdpat.setColor(QPalette::Normal,QPalette::WindowText,Qt::blue);
    ui->LCD_Value->setPalette(lcdpat);
    this->setAcceptDrops(true);
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->statescene.setBackgroundBrush(QBrush(Qt::blue));
    ui->GV_State->setScene(&this->statescene);
    //ui->LCD_Value->setToolTip("通道：");
}


SceneMark::SceneMark(const SceneMark &other) :
    QWidget(0),
    ui(new Ui::SceneMark)
{
    this->ChannelID=other.getChannelID();
    this->relativeX=other.RelativeX();
    this->relativeY=other.RelativeY();
}

//SceneMark::SceneMark(QWidget *parent) :
//    QWidget(parent),
//    ui(new Ui::SceneMark)
//{
//    ui->setupUi(this);
//    ui->LCD_Value->setSegmentStyle(QLCDNumber::Flat);
//    QPalette lcdpat = ui->LCD_Value->palette();
//    lcdpat.setColor(QPalette::Normal,QPalette::WindowText,Qt::blue);
//    ui->LCD_Value->setPalette(lcdpat);
//    this->setAcceptDrops(true);
//    this->setAttribute(Qt::WA_DeleteOnClose);
//    this->ChannelID=-1;
//    this->statescene.setBackgroundBrush(QBrush(Qt::blue));
//    ui->GV_State->setScene(&this->statescene);
//}

int SceneMark::getChannelID() const
{
    return this->ChannelID;
}

void SceneMark::setChannelID(int id)
{
    this->ChannelID=id;
    emit this->ChannelIDChanged(id);
    this->setToolTip(tr("ID:")+QString::number(id));
}

void SceneMark::setChannelPos(QString str)
{
    this->ui->L_Pos->setText(str);
    if(str=="")
        this->ui->L_Pos->hide();
    else
        this->ui->L_Pos->show();
}

void SceneMark::setValue(float value)
{
    this->ui->LCD_Value->display(value);
}

void SceneMark::setColor(int color)
{
    this->statescene.setBackgroundBrush(QBrush(color));
}

void SceneMark::setFontSize(int size)
{
    QFont font=ui->L_Pos->font();
    font.setPointSize(size);
    ui->L_Pos->setFont(font);
}

void SceneMark::setRelativeX(float x)
{
    this->relativeX=x;
}

void SceneMark::setRelativeY(float y)
{
    this->relativeY=y;
}



float SceneMark::RelativeX() const
{
    return this->relativeX;
}

float SceneMark::RelativeY() const
{
    return this->relativeY;
}

void SceneMark::setConfig()
{
    this->isconfig=true;
}

bool SceneMark::getConfig()
{
    return this->isconfig;
}

SceneMark &SceneMark::operator = (const SceneMark &other)
{
    int id=other.getChannelID();
    ChannelID=id;
    //this->setChannelID(other.getChannelID());
    this->setRelativeX(other.RelativeX());
    this->setRelativeY(other.RelativeY());
    return *this;
}

void SceneMark::ValueChanged(float value)
{
    this->setValue(value);
}

void SceneMark::StateChanged(int state)
{
    Qt::GlobalColor color=Qt::blue;
    switch(state)
    {
    case ST_OK:
    {
        color=Qt::green;
//        ui->LCD_Value->palette().setColor(QPalette::Normal,QPalette::WindowText,Qt::green);
//        this->statescene.setBackgroundBrush(QBrush(Qt::green));
        break;
    }
    case ST_DISCONN:
    {
        color=Qt::blue;
//        this->statescene.setBackgroundBrush(QBrush(Qt::blue));
        break;
    }
    case ST_HIGH:
    {
        color=Qt::red;
//        this->statescene.setBackgroundBrush(QBrush(Qt::red));
        break;
    }
    case ST_LOW:
    {
        color=Qt::yellow;
//        this->statescene.setBackgroundBrush(QBrush(Qt::yellow));
        break;
    }
    }

    QPalette lcdpat = ui->LCD_Value->palette();
    lcdpat.setColor(QPalette::Normal,QPalette::WindowText,color);
    //ui->LCD_Value->palette().setColor(QPalette::Normal,QPalette::WindowText,color);
    this->statescene.setBackgroundBrush(QBrush(color));
}



QDataStream & operator >> (QDataStream &in,SceneMark &data)
{
    in>>data.ChannelID>>data.relativeX>>data.relativeY;
    return in;
}

QDataStream & operator << (QDataStream &out,const SceneMark &data)
{
    out<<data.ChannelID<<data.relativeX<<data.relativeY;
    return out;
}

SceneMark::~SceneMark()
{
    delete ui;
}

void SceneMark::mouseDoubleClickEvent(QMouseEvent *)
{
    if(this->isconfig)
    {
        this->setChannelID(QInputDialog::getInt(this, "Input Dialog",
                                                "Please input your comment",this->ChannelID));
        emit newIDChanged(this->ChannelID);
        //emit this->ChannelIDChanged(this->ChannelID);
        this->ui->LCD_Value->display(this->ChannelID);
    }

}


